// pages/main/main.js
let app=getApp();
let API=require('../../api.js').API;
let util=require('../../utils/util');
let req=require('../../utils/request').Request;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    week:0
  },

  getOrders:function(){
    let token=app.globalData.token;
    let that=this;
    req.get(API.getOrders+'?token='+token)
    .then(function(res){
      console.log(res);
      that.setData({res:res});
    }).catch(function(res){
      console.log(res);
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
        let that=this;
        let scrollHeight=util.windowHeight(85);
        this.setData({scrollHeight:scrollHeight});
        req.get(API.getWeek)
        .then(function(res){
          console.log(res);
          if(res.code==0){
            that.setData({week:res.dataMap.week});
          }
        }).catch(function(res){
          console.log(res);
        })
  },

  /**
   * 监听headerbar里的tab
   */
  headtap: function(event){
    //获取订单数据
    this.getOrders();
    //console.log(this);
  },

  /**
   * 监听scroll-view刷新
   */
  refresh:function(event){
    let myComponent = this.selectComponent('#my-scrollview');
    console.log(myComponent);
    console.log(event);
    let res=event.detail;
    if(res.code==0){
      this.setData({res:res});
      wx.showToast({
        title:"刷新成功",
        icon:"success"
      });
    }else{
      wx.showToast({
        title:res.msg,
        icon:"error"
      });
    }
    myComponent.finish();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      //this.getOrders();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function (event) {
        
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});
